﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using OVS_FormsRecognition.Manager;
using System.Timers;

namespace OVS_FormsRecognition.Objects
{
    public class ThreadDetecor
    {

        #region Attr Logger
        /// <summary>
        /// Logger par défaut du ManagerUtilisateurs
        /// </summary>
        private static readonly log4net.ILog _Log = log4net.LogManager.GetLogger(typeof(ThreadDetecor));




        #endregion

        private Thread _Thread;

        public bool isStarted = false;

        public bool isFinished = false;

        public int id;

        public ThreadDetecor(int pId)
        {
            id = pId;
        }
       
        internal void Start(CameraFromMjpegDecoder camera)
        {
            _Thread = new Thread(ProcessFrame);
            _Thread.Start(camera);

            ProcessFrame(camera);
        }


        /// <summary>
        /// Processes the frame.
        /// </summary>
        /// <param name="stateInfo">The state info.</param>
        public void ProcessFrame(object param)
        {
            isStarted = true;

            CameraFromMjpegDecoder camera = param as CameraFromMjpegDecoder;

            if (FormsRecognitionManager.Instance.listCamera == null)
                return;
            if (FormsRecognitionManager.Instance.listCamera.Count <= 0)
                return;

            try
            {

                if (camera.currentFrame != null)
                {
                    ProcessDetectEventArgs detectEventArgs = new ProcessDetectEventArgs();
                    detectEventArgs.bitmapSource = camera.currentFrame;                    
                    detectEventArgs.camId = camera.CameraId;
                    detectEventArgs.detectObject = camera.detectObject;
                    detectEventArgs.ThreadId = camera.currentThreadIdInPool;
                    detectEventArgs.isInTestMode = camera.isInTestMode;

                    detectEventArgs.listHaarFilesToDetect = camera.HaartainningFilesListe;

                    FormsRecognitionManager.Instance.NewUpdateFromCamAllow(camera, detectEventArgs, camera.currentThreadIdInPool);
                  //  _Log.DebugFormat("UpdateCameraCapture(id: {0})", camera.CameraId);
                }

                // Forcing the CommandManager to raise the RequerySuggested event
                //CommandManager.InvalidateRequerySuggested();

            }
            catch (Exception ex)
            {
                //"UpdateCameraCapture: Exception: " + ex);
            }

            isFinished = true;
        }
    }
}
